# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 15:27:10 2018

@author: Who

嘻哈财经
http://www.xhcjtv.com/

"""
import requests
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def post_html_text(url, data):
    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': '12',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'PHPSESSID=brd1necs3li337hqunr5a6m2s5; Hm_lvt_e41038e895356ee5c3b1a632718b7ac4=1537492735,1537495264; Hm_lpvt_e41038e895356ee5c3b1a632718b7ac4=1537495268',
        'Host': 'www.xiha.top',
        'Origin': 'http://www.xiha.top',
        'Referer': 'http://www.xiha.top/newsflash.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_tt(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    text = eval(text)
    data = text['data']['list']
    dates = [date['published_time'][:10] for date in data]
    return dates


def get_dates_kx(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    text = eval(text)
    data = text['data']
    dates = [date['published_time'][:10] for date in data]
    return dates


def count_process_tt():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while a < 200:
        data = {
            'cid': '0',
            'stale_ids': '252,706,853',
            'page': a
        }
        url = 'http://www.xhcjtv.com/portal/index/ajaxgetarticle.html'
        print('----嘻哈财经头条获取第{}页----'.format(a))
        a += 1
        dates = get_dates_tt(url, data)
        if not dates:
            continue
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_kx():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while a < 200:
        data = {
            'cid': '2',
            'page': a
        }
        url = 'http://www.xhcjtv.com/portal/Article/ajaxGetArticle.html'
        print('----嘻哈财经快讯获取第{}页----'.format(a))
        a += 1
        dates = get_dates_kx(url, data)
        if not dates:
            continue
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


count_process_kx()


def main():
    u = '嘻哈财经'
    t1 = count_process_tt()
    t2 = count_process_kx()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
